﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ESF.SSO.Presenter.Client;
using ESF.Web.Pages;
using ESF.General;
using ESF.General.Entity;
using ESF.Web;
using System.Web.UI.HtmlControls;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.BusinessLogic;
using ESF.SSO.Web.Tools.Modules;

#endregion


public partial class SignUp : BasePagePresenter<SignUpPresenter>, ISignUpViewer
{
	#region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
	{
		_Presenter = new SignUpPresenter (this);
		
		if (!IsPostBack)
		{
			ListLoader.LoadList (ddlUserIdentificationType, WebGeneral.ListBasicReference (BasicReferenceTypes.IdentificationType));
			ListLoader.LoadList (ddlUserGender, WebGeneral.ListBasicReference (BasicReferenceTypes.Gender));
			ListLoader.LoadList (ddlUserSecurityQuestion, WebGeneral.ListBasicReference (BasicReferenceTypes.SecurityQuestion));

			_Presenter.Load ();

			#region Extra Pasos
			
			ifrStep_3.Visible = false;

			if (!DataHelper.IsEmpty (ExtraAppSteps))
			{
				ifrStep_3.Visible = true;

				JS.AddEndJS (Page, "ifrStep_3_IsValid = function (source, args) { var ifr = document.getElementById ('" + ifrStep_3.ClientID + "'); args.IsValid = ifr.contentWindow.IsValid (); }");

				cvlIframeOtherApps.ClientValidationFunction = "ifrStep_3_IsValid";

				// TODO: soportar mas de una aplicacion
				foreach (BasicReference app in ExtraAppSteps)
				{
					ifrStep_3.Attributes["src"] = RequestHelper.AppendVarValueToHTTPURL (app.Extra_I + "/" + app.Extra_II, PageHelper.KEY_SETDOCUMENTDOMAIN, AuthenticationModule.TicketDomain);
				}
			}
			else
			{
				cvlIframeOtherApps.Enabled = false;
			}

			#endregion
		}
	}
	
	#endregion
	
	
	#region Eventos de los botones
	
	protected void btnSignUp_Click(object sender, EventArgs e)
	{
		if (_Presenter.SignUp ())
		{
			btnSignUp.Enabled = false;

			JS.AddEndJS (Page, "function CompleteSignUp () { var ifrx = document.getElementById ('" + ifrStep_3.ClientID + "'); ifrx.contentWindow.SetUser ('" + UserID + "', mainURL); }; CompleteSignUp ();");
		}
	}
	
	#endregion


	#region Metodos de Autocompletado

	[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
	public static string[] QueryCountriesByName (string prefixText, int count)
	{
		return DataHelper.ConvertToStringArray (SignUpPresenter.QueryCountriesByName (prefixText, count));
	}


	[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
	public static string[] QueryCitiesByName(string prefixText, int count)
	{
		return DataHelper.ConvertToStringArray(SignUpPresenter.QueryCitiesByName(prefixText, count));
	}


	[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
	public static string[] QueryEnterprises(string prefixText, int count)
	{
		return WebGeneral.ListBasicReference (prefixText, count, BasicReferenceTypes.Enterprises);
	}
	
	
	#endregion


	#region Campos de ISignUpViewer

	public int UserID
	{
		set { ViewState ["UserID"] = value; }
		get { return (int) ViewState ["UserID"]; }
	}

	public string UserAlias
	{
		set { txtUserAlias.Text = value; }
		get { return txtUserAlias.Text; }
	}

	public string UserPassword
	{
		set { txtUserPassword.Text = value; }
		get { return txtUserPassword.Text; }
	}

	public string UserFirstName
	{
		set { txtUserFirstName.Text = value; }
		get { return txtUserFirstName.Text; }
	}

	public string UserSecondName
	{
		set { txtUserSecondName.Text = value; }
		get { return txtUserSecondName.Text; }
	}

	public string UserSurName
	{
		set { txtUserSurName.Text = value; }
		get { return txtUserSurName.Text; }
	}

	public string UserLastName
	{
		set { txtUserLastName.Text = value; }
		get { return txtUserLastName.Text; }
	}

	public int? UserIdentificationTypeID
	{
		set 
		{
			ListLoader.Select (ddlUserIdentificationType, value);
		}
		get 
		{
			return TextHelper.GetNullableInt (ddlUserIdentificationType.SelectedValue); 
		}
	}

	public string UserIdentificationNumber
	{
		set { txtUserIdentificationNumber.Text = value; }
		get { return txtUserIdentificationNumber.Text; }
	}

	public string UserEmail
	{
		set { txtUserEmail.Text = value; }
		get { return txtUserEmail.Text; }
	}

	public string UserAddress
	{
		set { txtUserAddress.Text = value; }
		get { return txtUserAddress.Text; }
	}

	public string UserLocationCountryName
	{
		set
		{
			txtUserLocationCountryName.Text = value;
		}
		get
		{
			return txtUserLocationCountryName.Text;
		}
	}	

	public string UserLocationCityName
	{
		set { txtUserLocationCityName.Text = value; } 
		get { return txtUserLocationCityName.Text; }
	}

	public int? UserSecurityQuestionID
	{
		set 
		{
			ListLoader.Select (ddlUserSecurityQuestion, value);
		}
		get { return TextHelper.GetInt (ddlUserSecurityQuestion.SelectedValue); }
	}

	public string UserSecurityQuestionAnswer
	{
		set { txtUserSecurityQuestionAnswer.Text = value; }
		get { return txtUserSecurityQuestionAnswer.Text; }
	}

	public string UserHomePhone
	{
		set { txtUserHomePhone.Text = value; }
		get { return txtUserHomePhone.Text; }
	}

	public string UserCellPhone
	{
		set { txtUserCellPhone.Text = value; }
		get { return txtUserCellPhone.Text; }
	}

	public string UserEnterprise
	{
		set { txtUserEnterprise.Text = value; }
		get { return txtUserEnterprise.Text; }
	}

	public int? UserGenderID
	{
		set 
		{ 
			ListLoader.Select (ddlUserGender, value);
		}
		get { return TextHelper.GetNullableInt (ddlUserGender.SelectedValue); }
	}
	
	public DateTime? UserBirthDate
	{
		set 
		{
			if (value == null) { txtUserBirthDate.Text = string.Empty; }
			else { txtUserBirthDate_CalendarExtender.SelectedDate = value; } 
		}
		get { return TextHelper.GetNullableDate (txtUserBirthDate.Text); }
	}
	
	
	public List<BasicReference> ExtraAppSteps
	{
		get
		{
			return ViewState ["ExtraAppSteps"] as List<BasicReference>;
		}
		set 
		{
			ViewState ["ExtraAppSteps"] = value;
		}  
	}


	public string UserProfession 
	{
		set { txtUserProfession.Text = value; }
		get { return txtUserProfession.Text; }
	}


	public string UserPosition 
	{
		set { txtUserPosition.Text = value; }
		get { return txtUserPosition.Text; }
	}

	#endregion
}
